package com.lianzhihui.romeex.ui.account;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.base.AppConfig;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.ImagesUtil;
import com.lianzhihui.romeex.utils.MyToast;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.QrUtils.EncodingHandler;
import com.lianzhihui.romeex.utils.QrUtils.QRUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 私钥二维码
 * Author: RAMON
 * CreateDate: 2020/9/22 4:10 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class PKQRActivity extends BaseActivity {

    @BindView(R.id.qr_pic_range)
    LinearLayout qr_pic_range;
    @BindView(R.id.qr_range)
    LinearLayout qr_range;
    @BindView(R.id.tv_pk)
    TextView tv_pk;
    @BindView(R.id.tv_pk2)
    TextView tv_pk2;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.iv_qrcode2)
    ImageView iv_qrcode2;
    @BindView(R.id.copy_bt)
    Button copy_bt;
    @BindView(R.id.save_qr)
    Button save_qr;

    String privateKey = "";
    Bitmap qrBitmap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pkqr_layout;
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.qr_code_pk));
        privateKey = getIntent().getStringExtra(AppConfig.PRIVATE_KEY);
        if (TextUtils.isEmpty(privateKey)) {
            MyToast.showToast("无数据");
        }
        loadData();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化 二维码，地址
     */
    private void loadData() {

        tv_pk.setText(privateKey);
        tv_pk2.setText(privateKey);
        //私钥
        String pk = QRUtils.type3 + privateKey;
        //显示 二维码
        ImagesUtil.createQrCodeImg(pk, iv_qrcode, 530);
        ImagesUtil.createQrCodeImg(pk, iv_qrcode2, 530);
        //保存二维码
        qrBitmap = EncodingHandler.createQRCode(pk, 530);

    }

    //生成二维码，显示私钥，保存二维码

    @SuppressLint("SetTextI18n")
    @OnClick({R.id.tv_pk, R.id.copy_bt, R.id.save_qr})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pk:
            case R.id.copy_bt:
                copyString(privateKey);
                break;
            case R.id.save_qr:
                //保存 图片
                if (NoDoubleClickUtils.isDoubleClick()) {
                    String[] PERMISSIONS = {
                            "android.permission.READ_EXTERNAL_STORAGE",
                            "android.permission.WRITE_EXTERNAL_STORAGE"};
                    //检测是否有写的权限
                    int permission = ContextCompat.checkSelfPermission(this,
                            "android.permission.WRITE_EXTERNAL_STORAGE");
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        // 没有写的权限，去申请写的权限，会弹出对话框
                        ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
                    } else {

//                        View qrView = LayoutInflater.from(this).inflate(R.layout.qr_range_layout, null);
                        View qrView = qr_range;

//                        ImageView qrImageview2 = qrView.findViewById(R.id.iv_qrcode2);
//                        TextView privateKey = qrView.findViewById(R.id.tv_pk2);
//                        qrImageview2.setImageBitmap(qrBitmap);
//                        privateKey.setText(privateKey + "");

                        ImagesUtil.saveQrCode(this, qrView);
                    }

                }
                break;
            default:
                break;
        }

    }

}
